package com.xiaoxu.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.auth.service.SysUserService;
import com.xiaoxu.common.result.Result;
import com.xiaoxu.common.utils.MD5;
import com.xiaoxu.model.system.SysUser;
import com.xiaoxu.vo.system.SysUserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xiaoxu
 * @since 2024-04-15
 */
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }

    //分页查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        SysUserQueryVo sysUserQueryVo){

        //1.先创建page对象
        Page<SysUser> pageParams = new Page<>(page,limit);
        //2.判断封装条件是否为空?
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();

        String keyWord = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        if (!StringUtils.isEmpty(keyWord)){

            wrapper.like(SysUser::getUsername,keyWord);
        }
        // //ge 大于等于 greater
        if (!StringUtils.isEmpty(createTimeBegin)){
            wrapper.ge(SysUser::getCreateTime,createTimeBegin);
        }
        // //le 小于等于
        if (!StringUtils.isEmpty(createTimeEnd)){
            wrapper.le(SysUser::getCreateTime,createTimeEnd);
        }
        //3.调用service方法
        //调用service方法
        Page<SysUser> pageModel = sysUserService.page(pageParams, wrapper);
        return Result.ok(pageModel);
    }

    //根据用户id查询用户详情
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysUser user = sysUserService.getById(id);
        return Result.ok(user);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    //添加
    @PostMapping("save")
    public Result add(@RequestBody SysUser sysUser){
        String passwordMD5 = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(passwordMD5);
        sysUserService.save(sysUser);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    //修改
    @PutMapping("update")
    public Result updateById(@RequestBody SysUser sysUser){

        sysUserService.updateById(sysUser);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    //删除
    @DeleteMapping("remove/{id}")
    public Result delete(@PathVariable Long id){
        sysUserService.removeById(id);
        return Result.ok();
    }

}

